package View;

import Controller.LoginController;
import pojo.AppData;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Login_window extends JFrame{

    private String userName ;
    private String pwd ;
    private String role ;
    private Map<String,String> userMap = new HashMap<>();

    // 主窗口
    private Main_window main_window;
    private UserMain_window userMain_window;

    // 登录控制
    private LoginController loginController;

    {
        try {
            loginController = new LoginController();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JFrame frame = new JFrame("登录窗口");
    private JPanel backgroundPanel = new JPanel(); // 包含背景图片的面板

    private JLabel lblUser = new JLabel("用户名:");
    private JLabel lblPwd = new JLabel("密码:");
    private JLabel lblRole = new JLabel("角色:");

    private JTextField txtUser = new JTextField(15);
    private JPasswordField txtPwd = new JPasswordField(15);

    private JComboBox<String> roleComboBox = new JComboBox<>(new String[]{"管理员", "普通用户"});
    private JButton blogin = new JButton("登录");
    private JButton bregister = new JButton("注册");


    // 构造方法
    public Login_window() {

    }

    public void Login_window_start(){
        frameControl();
    }

    // 设置容器管理
    public void frameControl(){
        // 设置底层容器和布局管理器
        frame.setSize(500, 490);
        frame.setLocation(600, 200);
        frame.setResizable(false);

        //设置字体
        MysetFont();

        // 设置背景图片
        backgroundPanel.setLayout(new BorderLayout());
        ImageIcon background = new ImageIcon("src/main/resources/picture/back1.jpg");
        JLabel backgroundLabel = new JLabel(background);
        backgroundPanel.add(backgroundLabel, BorderLayout.CENTER);
        frame.setContentPane(backgroundPanel);

        //进行布局排版
        MyLayout();

        //事件处理
        eventTake();

        // 设置窗口可见性和关闭操作
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //事件处理
    public void eventTake(){
        // 添加登录按钮的点击事件监听器
        blogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理登录按钮点击事件
                // 获取用户名和密码,角色
                userMap = getUserAndPwd();
                userName = userMap.get("username");
                pwd = userMap.get("password");
                role = userMap.get("role");

                // 登录逻辑
                try {
                    //判断角色
                    //1.管理员
                    if (role.equals("管理员")){
                        int flag = loginController.controllerAdministratorsLogin(userName,pwd);
                        if (flag!=-1){
                            AppData.setUserId(flag);
                            System.out.println("管理员登录成功: 管理员"+flag);
                            //弹出提示登陆成功信息
                            JOptionPane.showMessageDialog(Login_window.this, "管理员登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                            //将登录窗口隐藏
                            frame.setVisible(false);
                            //打开主窗口
                            new Main_window();
                        }else {
                            System.out.println("管理员登陆失败");
                            //弹出提示登陆失败信息
                            JOptionPane.showMessageDialog(Login_window.this, "管理员登录失败，请检查用户名和密码！", "失败", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    //2.普通用户
                    if (role.equals("普通用户")){
                        int flag = loginController.controllerUserLogin(userName,pwd);
                        if (flag!=-1){
                            AppData.setUserId(flag);
                            System.out.println("用户登录成功: 用户"+flag);
                            //弹出提示登陆成功信息
                            JOptionPane.showMessageDialog(Login_window.this, "用户登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                            //将登录窗口隐藏
                            frame.setVisible(false);
                            //打开用户窗口
                            new UserMain_window();
                        }else {
                            System.out.println("普通用户登陆失败");
                            //弹出提示登陆失败信息
                            JOptionPane.showMessageDialog(Login_window.this, "用户登录失败，请检查用户名和密码！", "失败", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });

        // 添加注册按钮的点击事件监听器
        bregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 处理注册按钮点击事件
                // 获取用户名和密码,角色
                userMap = getUserAndPwd();
                userName = userMap.get("username");
                pwd = userMap.get("password");
                role = userMap.get("role");

                // 注册逻辑
                try{
                    //判断角色
                    //1.管理员
                    if (role.equals("管理员")){
                        Boolean flag = loginController.controllerAdministratorsRegister(userName,pwd);
                        if (flag){
                            System.out.println("管理员注册成功");
                            //弹出提示注册成功信息
                            JOptionPane.showMessageDialog(Login_window.this, "管理员注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                        }else {
                            System.out.println("管理员注册失败");
                            //弹出提示注册失败信息
                            JOptionPane.showMessageDialog(Login_window.this, "管理员注册失败，用户名已存在！", "失败", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    //2.普通用户
                    if (role.equals("普通用户")){
                        String registrationDate = getCurrentTimestamp();
                        Boolean flag = loginController.controllerUserRegister(userName,pwd,registrationDate);
                        if (flag){
                            System.out.println("普通用户注册成功");
                            //弹出提示注册成功信息
                            JOptionPane.showMessageDialog(Login_window.this, "普通用户注册成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                        }else {
                            System.out.println("普通用户注册失败");
                            //弹出提示注册失败信息
                            JOptionPane.showMessageDialog(Login_window.this, "普通用户注册失败，用户名已存在！", "失败", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                }catch (Exception exception){
                    exception.printStackTrace();
                }
            }
        });
    }

    // 获取当前时间
    private String getCurrentTimestamp() {
        // 获取当前时间
        Date d = new Date();

        long time = d.getTime();

        // 2、格式化日期对象，和时间 毫秒值。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedTimestamp = dateFormat.format(time);

        return formattedTimestamp;
    }

    //进行布局排版
    public void MyLayout(){
        // 设置登录窗口布局
        JPanel loginPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // 用户名标签和文本框
        gbc.gridx = 0;
        gbc.gridy = 0;
        loginPanel.add(lblUser, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        loginPanel.add(txtUser, gbc);

        // 密码标签和文本框
        gbc.gridx = 0;
        gbc.gridy = 1;
        loginPanel.add(lblPwd, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        loginPanel.add(txtPwd, gbc);

        // 角色选择列表
        gbc.gridx = 0;
        gbc.gridy = 2;
        loginPanel.add(lblRole, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        loginPanel.add(roleComboBox, gbc);

        // 登录和注册按钮
        gbc.gridx = 0;
        gbc.gridy = 3;
        loginPanel.add(blogin, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        loginPanel.add(bregister, gbc);

        // 添加登录面板到背景面板
        backgroundPanel.add(loginPanel, BorderLayout.SOUTH);
    }

    //设置字体
    public void MysetFont(){
        lblUser.setFont(new Font("隶书",Font.BOLD,20));
        lblPwd.setFont(new Font("隶书",Font.BOLD,20));
        lblRole.setFont(new Font("隶书",Font.BOLD,20));
        roleComboBox.setFont(new Font("隶书",Font.BOLD,15));
        blogin.setFont(new Font("隶书",Font.BOLD,20));
        bregister.setFont(new Font("隶书",Font.BOLD,20));
    }

    //用户名和密码,角色
    public Map<String,String> getUserAndPwd(){
        String username = txtUser.getText();
        String pwd = new String(txtPwd.getPassword());
        String role = (String) roleComboBox.getSelectedItem();

        Map<String,String> maps = new HashMap<>();
        maps.put("username",username);
        maps.put("password",pwd);
        maps.put("role",role);
        return maps;
    }

}

